<template>
  <div class="employee-container">
    <!-- 搜索区域 -->
    <!--
      搜索功能：前端吧当前用户输入的关键词传给后端 后端用这个关键词做过滤筛选 把符合条件的返回给前端
      1.双向绑定拿到关键词
      2.使用最新的关键词字段数据重新获取列表

      搜索： 1.精确搜索（name完全等于用户名字才可以搜索） 2.模糊匹配（只要name中包含即可不需要精确搜索）
       -->
    <div class="search-container">
      <div class="search-label">员工姓名：</div>
      <el-input v-model="params.name" clearable placeholder="请输入内容" class="search-main" @clear="clearSearch" />
      <el-button type="primary" @click="getEmployeeList">查询</el-button>
    </div>
    <div class="create-container">
      <el-button type="primary" @click="addEmployee">添加员工</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="employeeList">
        <el-table-column type="index" label="序号" />
        <el-table-column label="员工姓名" width="180" prop="name" />
        <el-table-column label="登录账号" width="180" prop="userName" />
        <el-table-column label="联系方式" width="180" prop="phonenumber" />
        <el-table-column label="角色" width="180" prop="roleName" />
        <el-table-column label="状态" width="180">
          <template #default="scope">
            {{ scope.row.status === 0 ?'已启用': '未启用' }}
          </template>
        </el-table-column>
        <el-table-column label="添加时间" prop="createTime" />
        <el-table-column label="操作" fixed="right" width="180">
          <template #default="scope">
            <el-button size="mini" type="text" @click="edit(scope.row)">编辑</el-button>
            <el-button size="mini" type="text" @click="delEmployee(scope.row.id)">删除</el-button>
            <el-button size="mini" type="text">重置密码</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <!-- 分页 -->
      <el-pagination
        layout="prev, pager, next"
        :total="total"
        :page-size="params.pageSize"
        @current-change="pageChange"
      />
    </div>
    <!-- 添加员工 -->
    <el-dialog
      :title="id?'编辑员工':'添加员工'"
      :visible="dialogVisible"
      width="480px"
      @close="closeDialog"
      @open="openDialog"
    >
      <!-- 表单接口 -->
      <div class="form-container">
        <el-form ref="addForm" :model="addForm" :rules="addFormRules" label-width="80px">
          <el-form-item label="员工姓名" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>
          <el-form-item label="登录账号" prop="userName">
            <el-input v-model="addForm.userName" />
          </el-form-item>
          <el-form-item label="联系方式" prop="phonenumber">
            <el-input v-model="addForm.phonenumber" />
          </el-form-item>
          <el-form-item label="分配角色" prop="roleId">
            <el-select v-model="addForm.roleId" placeholder="请选择角色">
              <el-option
                v-for="item in roleList"
                :key="item.roleId"
                :label="item.roleName"
                :value="item.roleId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="员工状态" prop="status">
            <el-radio-group v-model="addForm.status">
              <el-radio :label="0">未启用</el-radio>
              <el-radio :label="1">启用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="createEmployee">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getEmployeeListAPI, createEmployeeAPI, delEmployeeAPI, editEmployeeAPI, gainEmployeeListAPI } from '@/api/employee'
import { getRoleListAPI } from '@/api/role'
export default {
  name: 'Employee',
  data() {
    return {
      id: '',
      dialogVisible: false,
      total: 100, // 总数
      page: '',
      employeeList: [],
      params: {
        page: 1, // 当前页码
        pageSize: 10, // 每页条数
        name: '' // 员工姓名
      },
      roleList: [], // 角色列表
      // 表单
      addForm: {
        name: '',
        phonenumber: '',
        roleId: '',
        status: 1,
        userName: ''
      },
      // 验证规则
      addFormRules: {
        name: [
          { required: true, message: '请输入员工姓名', trigger: 'blur' }
        ],
        userName: [
          { required: true, message: '请输入登录账号', trigger: 'blur' }
        ],
        phonenumber: [
          { required: true, message: '请输入联系方式', trigger: 'blur' }
        ],
        roleId: [
          { required: true, message: '请分配角色', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择员工状态', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.getEmployeeList()
  },
  methods: {
    async getEmployeeList() {
      const res = await getEmployeeListAPI(this.params)
      this.employeeList = res.data.rows
    },
    // 分页
    pageChange(page) {
      this.params.page = page
      this.getEmployeeList()
    },
    // 关闭弹框
    closeDialog() {
      this.dialogVisible = false
      // 重置表单
      // this.id = ''
      this.addForm = {
        poleName: '',
        poleNumber: '',
        poleIp: '',
        areaId: '',
        poleType: ''
      }
      this.$refs.addForm.resetFields()
    },
    // 打开弹框
    addEmployee() {
      this.dialogVisible = true
      delete this.id
      this.getEmployeeList()
    },
    // 确认添加
    createEmployee() {
      this.$refs.addForm.validate(async valid => {
        console.log(22, this.addForm)
        if (valid) {
          if (!this.id) {
            // 调用添加 接口
            await createEmployeeAPI(this.addForm)
          } else {
            console.log('这个是id', this.id)
            await editEmployeeAPI(this.addForm)
            // 添加完成
          }

          // 2.渲染新添加的东西 调用方法
          this.getEmployeeList()
          // 1.点击确定关闭弹窗组件
          this.dialogVisible = false
        }
      })
    },
    // 弹窗 下拉列表 角色分配
    async openDialog() {
      const res = await getRoleListAPI()
      this.roleList = res.data
      console.log(open, this.roleList)
    },
    // 删除
    delEmployee(id) {
      this.$confirm('删除员工后将不可登录，确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delEmployeeAPI(id)
        this.$message({
          type: 'success',
          message: '删除成功'
        })
        this.getEmployeeList()
      })
    },
    // 修改员工
    async edit(row) {
      this.id = row.id
      this.dialogVisible = true
      const res = await gainEmployeeListAPI(row.id)
      console.log(res)
      const { name, phonenumber, roleId, status, userName, id } = res.data
      this.addForm = { name, phonenumber, roleId, status, userName, id }
      this.getEmployeeList()
    },
    clearSearch() {
      // 把当前的请求页数 复原为1 然后再请求
      this.params.page = 1
      this.getEmployeeList()
    }
  }
}
</script>

<style lang="scss" scoped>
.employee-container {
  padding: 10px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  ;
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
.form-container{
  padding:0px 80px;
}
</style>
